package com.hj.security.ui;

import com.hj.security.R;
import com.hj.security.receiver.MyAdminReceiver;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class SetupGuide4Activity extends Activity implements OnClickListener{

	private CheckBox cb_guide_protected;
	private Button bt_guide_previous;
	private Button bt_guide_finish;
	private SharedPreferences sp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setup_guide4);
		cb_guide_protected = (CheckBox) findViewById(R.id.cb_guide_protected);
		bt_guide_previous = (Button) findViewById(R.id.bt_guide_previous);
		bt_guide_finish = (Button) findViewById(R.id.bt_guide_finish);
		
		bt_guide_previous.setOnClickListener(this);
		bt_guide_finish.setOnClickListener(this);
		
		sp = this.getSharedPreferences("config", this.MODE_PRIVATE);
		boolean isProtecting  = sp.getBoolean("isProtected", false);
		
		if(isProtecting==true){
			cb_guide_protected.setText("已经开启保护");
			cb_guide_protected.setChecked(isProtecting);
		}
		
		cb_guide_protected.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					cb_guide_protected.setText("已经开启保护");
					cb_guide_protected.setChecked(isChecked);
				}else{
					cb_guide_protected.setText("没有开启保护");
					Editor editor = sp.edit();
					editor.putBoolean("isProtected", false);
					editor.commit();
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.bt_guide_finish:
			if(cb_guide_protected.isChecked()){
				finishSetupGuide();
				finish();
			}else{
				AlertDialog.Builder builder = new Builder(this);
				builder.setTitle("提醒");
				builder.setMessage("强烈建议您开启保护, 是否完成设置");
				builder.setCancelable(false);
				builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						finishSetupGuide();
						finish();
					}
				});
				builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Editor editor = sp.edit();
						editor.putBoolean("setupGuide", true);
						editor.commit();
					}
				});
				builder.show();
			}
			break;
		case R.id.bt_guide_previous:
			intent = new Intent(this, SetupGuide3Activity.class);
			finish();
			startActivity(intent);
			intent = null;
			overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out); 		
			break;

		default:
			break;
		}
	}

	private void finishSetupGuide() {
		Editor editor = sp.edit();
		editor.putBoolean("setupGuide", true);
		editor.commit();
		
		//拿到一个设备管理器
		DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
		//new一个新的组件出来，用来启动注册管理器的界面
		ComponentName componentName = new ComponentName(this, MyAdminReceiver.class);
		//判断是否已经注册，没有就进行注册
		if(!devicePolicyManager.isAdminActive(componentName)){
			Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
			intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
			startActivity(intent);
		}
	}
}
